package www.woniuxy.com.class112.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import www.woniuxy.com.class112.dao.*;
import www.woniuxy.com.class112.dto.IssuesDto;
import www.woniuxy.com.class112.entity.Issues;
import www.woniuxy.com.class112.entity.SpecialAgents;
import www.woniuxy.com.class112.entity.WorkOrders;
import www.woniuxy.com.class112.entity.WorkUserImage;
import www.woniuxy.com.class112.param.issues.AdminAddIssuesParam;
import www.woniuxy.com.class112.param.issues.AssignAgentParam;
import www.woniuxy.com.class112.param.issues.SelectIssuesParam;
import www.woniuxy.com.class112.service.IssuesService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class IssuesServiceImpl implements IssuesService {
    @Resource
    private IssuesMapper issuesMapper;
    @Resource
    private WorkOrdersMapper workOrdersMapper;
    @Resource
    private SpecialAgentsMapper specialAgentsMapper;
    @Resource
    private WorkUserImageMapper workUserImageMapper;
    @Override
    public PageInfo<IssuesDto> selectIssues(SelectIssuesParam param) {
        PageHelper.startPage(param.getPageIndex() == null ? 1 : param.getPageIndex(), param.getPageSize() == null ? 3 : param.getPageSize());
        List<IssuesDto> page = issuesMapper.selectIssuesDto(param.getIssueType(), param.getIssueState(), param.getStartTime(), param.getStartTime(),param.getDescription());

        for (IssuesDto issuesDto : page) {
            if (issuesDto.getResolvePeople()!=null&&issuesDto.getResolvePeople()==0){
                issuesDto.setResolveName("知识库");
            }
        }
        System.out.println(page);
        return new PageInfo<IssuesDto>(page) ;
    }

    @Override
    public void addIssue(AdminAddIssuesParam form) {
        System.out.println(form);
        Issues issues = BeanUtil.toBean(form, Issues.class);
        Snowflake snowflake = new Snowflake(1, 1);
        long l = snowflake.nextId();
        issues.setIssueNumber(l);
        issues.setTime(new Date());
        issues.setIssueState(0L);
        issuesMapper.insertSelective(issues);
        WorkUserImage workUserImage = new WorkUserImage();
        workUserImage.setUserId(issues.getUserId());
        workUserImage.setIssueId(issues.getId());
        workUserImage.setUserImage(issues.getImageUrl());
        workUserImageMapper.insertSelective(workUserImage);

    }
    @Override
    public void assignAgent(AssignAgentParam param) {
        Issues issues = issuesMapper.selectByPrimaryKey(param.getIssuesId());
        if (issues==null){
            throw new RuntimeException("该问题不存在");
        }
        if (issues.getAgentId()!=null){
            throw new RuntimeException("该问题已分配");
        }
        SpecialAgents specialAgents = specialAgentsMapper.selectByPrimaryKey(param.getAgentId());
        if (specialAgents==null){
            throw new RuntimeException("该特派员不存在");
        }
        WorkOrders workOrders = new WorkOrders();
        workOrders.setWorkNumber((new Snowflake(1, 1)).nextId());//工单编号
        workOrders.setOrderTitle("管理人员指派");//标题
        workOrders.setIssueId(issues.getId());//问题表id
        workOrders.setUserId(issues.getUserId());//服务对象id

        workOrders.setAgentId(specialAgents.getUserId());//处理工单的特派员
        workOrders.setIssueTypeId(issues.getIssueType());//问题类型
        workOrders.setStateId(1L);
        workOrders.setWorkType(2L);
        workOrders.setCreatedDate(new Date());//创建时间
        workOrdersMapper.insertSelective(workOrders);
        System.out.println(workOrders.getId());

        WorkUserImage workUserImage = new WorkUserImage();//添加图片到工单
        workUserImage.setUserId(issues.getUserId());//服务对象id
        workUserImage.setIssueId(issues.getId());//问题表id
//        workUserImage.setUserImage(issues.getImageUrl());//图片
        workUserImageMapper.insertSelective(workUserImage);

        issues.setIssueState(1L);
        issues.setOrderId(workOrders.getId());
        issues.setResolvePeople(param.getAgentId());
        issues.setAgentId(specialAgents.getUserId());
        issuesMapper.updateByPrimaryKeySelective(issues);



    }
}
